package com.itsu.config;

import com.itsu.compoment.socket.MyWebSocketHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerView;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

import java.util.Arrays;
import java.util.List;
import java.util.Properties;

/**
 * @author 苏犇
 * @create time 2019/12/4 21:09
 */
@Configuration
@ComponentScan(basePackages = "com.itsu.controller")
@EnableWebMvc
@EnableAspectJAutoProxy
@EnableWebSocket
public class WebMvcConfig implements WebMvcConfigurer, WebSocketConfigurer {
    @Bean
    public FreeMarkerViewResolver freeMarkerViewResolver() {
        FreeMarkerViewResolver markerViewResolver =
                new FreeMarkerViewResolver();
        markerViewResolver.setSuffix(".html");
        markerViewResolver.setOrder(1);
        markerViewResolver.setCache(false);
        markerViewResolver.setViewClass(FreeMarkerView.class);
        markerViewResolver.setContentType("text/html;charset=utf-8");
        return markerViewResolver;
    }

    @Bean
    public FreeMarkerConfigurer freeMarkerConfigurer() {
        FreeMarkerConfigurer freeMarkerConfigurer =
                new FreeMarkerConfigurer();
        freeMarkerConfigurer.setTemplateLoaderPath("/WEB-INF/views/");

        Properties settingProperties = new Properties();
        // 刷新模板的周期，单位为秒
        settingProperties.setProperty("template_update_delay", "5");
        settingProperties.setProperty("default_encoding", "utf-8");
        settingProperties.setProperty("datetime_format", "yyyy-MM-dd HH:mm:ss");
        settingProperties.setProperty("time_format", "HH:mm:ss");
        settingProperties.setProperty("url_escaping_charset", "utf-8");

        freeMarkerConfigurer.setFreemarkerSettings(settingProperties);

        return freeMarkerConfigurer;
    }

    //文件上传，bean必须写name属性且必须为multipartResolver，不然取不到文件对象
    @Bean(name = "multipartResolver")
    public CommonsMultipartResolver multipartResolver() {
        CommonsMultipartResolver multipartResolver =
                new CommonsMultipartResolver();
        multipartResolver.setMaxUploadSize(2097152); //2M
        //单个文件大小限制
        multipartResolver.setMaxUploadSizePerFile(0);
        multipartResolver.setDefaultEncoding("UTF-8");
        return multipartResolver;
    }

    //静态资源的处理
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/home").setViewName("home");
        registry.addViewController("/index").setViewName("index");
        registry.addViewController("/").setViewName("login");
        registry.addViewController("/login").setViewName("login");
        registry.addViewController("/submsg").setViewName("submsg");

    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter msgConverter = new MappingJackson2HttpMessageConverter();
        msgConverter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON_UTF8, MediaType.TEXT_HTML, MediaType.APPLICATION_FORM_URLENCODED));
        converters.add(msgConverter);
    }


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("/");
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(webSocketHandler(), "/websocket").addInterceptors(new HttpSessionHandshakeInterceptor()).setAllowedOrigins("*");
    }

    @Bean
    public WebSocketHandler webSocketHandler() {
        return new MyWebSocketHandler();
    }
}
